/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bookshop.dao;

import com.bookshop.entity.Author;
import com.bookshop.exception.EBookshopException;
import java.util.List;
import javax.ejb.Singleton;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Caroline
 */
@Singleton(mappedName = "com.bookshop.dao.author")
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class DaoAuthorImpl implements IDaoAuthorLocal, IDaoAuthorRemote {
    
    @PersistenceContext
    private EntityManager em; 

    @Override
    public List<Author> getAllAuthorsByBookId(int bookId) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Author> getAuthorByName(String name) {
        try {
            List<Author> authorList = em.createNamedQuery("Author.findByFirstName").setParameter("firstName", "%"+name+"%").getResultList();
            List<Author> authorListBis = em.createNamedQuery("Author.findByLastName").setParameter("lastName", "%"+name+"%").getResultList();
            
            for (Author a : authorListBis){
                if (!authorList.contains(a)){
                    authorList.add(a);
                }
            }

            return authorList;
        } catch(Throwable th) {
            throw new EBookshopException(th.getMessage(), th);
        }
    }
    
}
